package com.sdry.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysDept;
import com.sdry.system.service.DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: info_sdry
 * @description: 部门管理
 * @author: cb
 * @create: 2020-02-17 14:05
 **/
@Controller
@RequestMapping(value = "dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    /**
     * 展示
     * @param model
     * @return
     */
    @RequestMapping("/showDept")
    public String showDept(Model model) {
        return "system/dept/deptList";
    }

    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "获取部门列表",notes = "分页查询")
    @ApiImplicitParam(name = "showDeptPage",value = "部门实体SysDept",required = true,dataType = "SysDept")
    @RequestMapping(value = "showDeptPage",method = RequestMethod.GET)
    @ResponseBody
    public TableDataInfo showRolePage(SysDept sysDept) {
        logger.info("获取部门列表 sysDept = " + JSONObject.fromObject(sysDept));
        Page<SysDept> page = new Page<>(sysDept.getPage(),sysDept.getLimit());
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(sysDept.getDeptName())) {
            queryWrapper.eq("del_flag",0L);
        }else {
            queryWrapper.eq("del_flag",0L).like("dept_name", sysDept.getDeptName());
        }
        IPage<SysDept> sysDeptIPage = deptService.page(page,queryWrapper);
        return getDataTable(sysDeptIPage.getRecords(), sysDeptIPage.getTotal());
    }

    /**
     * 新增
     * @param sysDept
     * @return
     */
    @RequestMapping(value = "addDept")
    @ResponseBody
    public boolean addDept(SysDept sysDept) {
        return deptService.saveOrUpdate(sysDept);
    }

    /**
     * 去编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "updateDept" )
    @ResponseBody
    public String updateDept(Long id) {
        SysDept sysDept = null;
        if (id != null){
            sysDept = deptService.getById(id);
        }
        return JSONObject.fromObject(sysDept).toString();
    }

    /**
     * 删除
     * @param deptId 主键ID
     * @return
     */
    @RequestMapping(value = "deleteDept")
    @ResponseBody
    public boolean deleteRole(Long deptId) {
        return deptService.deleteDept(deptId);
    }
}
